package com.spring.dao.impl;

import com.spring.dao.AccountDao;
import com.spring.domain.Account;
import com.spring.util.ConnectionHolder;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

@Repository
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private QueryRunner queryRunner;
    @Autowired
    private ConnectionHolder connectionHolder;

    @Override
    public void save(Account account) {
        String sql = "insert into account values(null,?,?)";

        try {
            queryRunner.update(sql, account.getName(), account.getMoney());
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void delById(int id) {

    }

    @Override
    public Account findById(int id) {
        return null;
    }

    @Override
    public void update(String name, double money) {
        String sql = "update account set money = money + ? where name = ?";

        try {
            queryRunner.update(connectionHolder.getConnection(), sql, money, name);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }
}
